package com.haier.nengyuan.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import java.util.Calendar;

public class EnergyTotalActivity extends BaseActivity implements View.OnClickListener {
    private ImageView mTitleLeft;
    private TextView mTitleCenter;
    private TextView mTitleRight;

    private TextView mTvTimeDetail;
    private RadioButton mRbDay;
    private RadioButton mRbWeek;
    private RadioButton mRbMonth;
    private RadioButton mRbYear;
    private String mMonth;
    private String mMonth1;
    private String mStrDay;

    private int mDay;
    private int mDayStart;
    private int mDayEnd;
    private int mDayEnd1;
    private long time = System.currentTimeMillis();
    private long time1 = System.currentTimeMillis() + 604800000;

    private DayEnergyFragment01 mBarChartFragment = new DayEnergyFragment01();
    private WeekEnergyFragment01 mWeekBarChartFragment = new WeekEnergyFragment01();
    //    private DayEnergyFragment mBarChartFragment = new DayEnergyFragment();
//    private WeekEnergyFragment mWeekBarChartFragment = new WeekEnergyFragment();
    private MonthEnergyFragment mMonthFragment = new MonthEnergyFragment();
    private YearEnergyFragment mYearFragment = new YearEnergyFragment();

    private BaseFragment[] mAllFrags = {mBarChartFragment, mWeekBarChartFragment, mMonthFragment, mYearFragment};
    private String mMonth2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_energy_total);
        initViews();
        initDatas();
    }

    private void initViews() {
        mTvTimeDetail = (TextView) findViewById(R.id.tv_time_detail);
        mRbDay = (RadioButton) findViewById(R.id.rb_day);
        mRbWeek = (RadioButton) findViewById(R.id.rb_week);
        mRbMonth = (RadioButton) findViewById(R.id.rb_month);
        mRbYear = (RadioButton) findViewById(R.id.rb_year);
        mTitleLeft = (ImageView) findViewById(R.id.title_left);
        mTitleCenter = (TextView) findViewById(R.id.title_center);
        mTitleRight = (TextView) findViewById(R.id.title_right);

        mRbDay.setOnClickListener(this);
        mRbWeek.setOnClickListener(this);
        mRbMonth.setOnClickListener(this);
        mRbYear.setOnClickListener(this);
        mTitleLeft.setOnClickListener(this);
        mTitleCenter.setOnClickListener(this);
        mTitleRight.setOnClickListener(this);
    }

    private void initDatas() {
        mRbDay.setChecked(true);
        addFragment(R.id.chart_fragments, mBarChartFragment);
        mTvTimeDetail.setText("0时 - 24时");
        mMonth = MyDateUtil.changeM(time);
        mMonth1 = MyDateUtil.changeM(time1);
        mMonth2 = MyDateUtil.changeM(time);//获取当前月
        mStrDay = MyDateUtil.changeD(time);
        int mIntDay = Integer.parseInt(mStrDay);
        int i = Integer.parseInt(mMonth);
        if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) {
            mDay = 31;
        } else {
            mDay = 30;
        }
        Calendar rightNow = Calendar.getInstance();
        int week = rightNow.get(rightNow.DAY_OF_WEEK) - 1;//获取时间

//        mDayStart = mIntDay - week + 1;
        if (week == 0) {
            week = 7;
        }
        if (mIntDay < week) {
            int j = i - 1;
            if (j == 1 || j == 3 || j == 5 || j == 7 || j == 8 || j == 10 || j == 12) {
                mDay = 31;
            } else {
                mDay = 30;
            }
            mDayStart = mIntDay + mDay - week + 1; //////
            mMonth = (Integer.parseInt(mMonth)) - 1 + "";
        } else {
            mDayStart = mIntDay - week + 1;/////
        }


        mDayEnd = mIntDay + (7 - week);
        if (mDayEnd > 31) {
            mDayEnd1 = mDayEnd - 31;
        } else {
            mDayEnd1 = mDayEnd;
        }
        mTitleCenter.setText("累计发电量");
        mTitleRight.setText("");


    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.rb_day) {
            mTvTimeDetail.setText("0时 - 24时");
            showFrag(mBarChartFragment);
        }
        if (id == R.id.rb_week) {
            /*xinming start*/
            Calendar rightNow = Calendar.getInstance();
            int week = rightNow.get(rightNow.DAY_OF_WEEK) - 1;
            if (week == 0) {
                week = 7;
            }
            long bef = (week - 1) * 24 * 60 * 60 * 1000;
            long later = (7 - week) * 24 * 60 * 60 * 1000;

            mTvTimeDetail.setText(MyDateUtil.changeM(time - bef) +
                    "月" + MyDateUtil.changeD(time - bef) + "日" + " - " +
                    MyDateUtil.changeM(time + later) + "月" + MyDateUtil.changeD(time + later) +
                    "日" );
            /*xinming end*/
            showFrag(mWeekBarChartFragment);
        }
        if (id == R.id.rb_month) {
            mTvTimeDetail.setText("1月1日" +
                    " - " + MyDateUtil.changeM(time) +
                    "月" +
                    MyDateUtil.changeD(time) +
                    "日");

            showFrag(mMonthFragment);
        }
        if (id == R.id.rb_year) {
            long time = System.currentTimeMillis();
            int year = Integer.valueOf(MyDateUtil.changeY(time));
            if (year == 2017) {
                mTvTimeDetail.setText("2017年");
            } else if (year == 2018) {
                mTvTimeDetail.setText("2018年");
            }
//            mTvTimeDetail.setText("2017年");
            showFrag(mYearFragment);
        }
        if (id == R.id.title_left) {
            finish();
        }


    }

    public void showFrag(BaseFragment desFragment) {
        for (BaseFragment baseFragment : mAllFrags) {
            if (baseFragment == desFragment) {
                if (!desFragment.isAdded()) {
                    addFragment(R.id.chart_fragments, desFragment);
                }
                showFragment(desFragment);

            } else {
                hideFragment(baseFragment);
            }
        }
    }
}
